package com.ty.store.controller;

import com.ty.store.service.ICartService;
import com.ty.store.util.JsonResult;
import com.ty.store.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author ty
 */
@RequestMapping("carts")
@RestController
public class CartController extends BaseController{
    @Autowired
    private ICartService iCartService;
    @RequestMapping("add_to_cart")
    public JsonResult<Void> addToCart(Integer pid, Integer amount, HttpSession session){
        Integer uid = getuidFromSession(session);
        String username = getUsernameFromSession(session);
        // 调用业务对象执行添加到购物车
        iCartService.addToCart(uid,pid,amount,username);
        // 返回成功
        return new JsonResult<Void>(OK);
    }
    @RequestMapping({"", "/"})
    public JsonResult<List<CartVo>> getVoByUid(HttpSession session){
        List<CartVo> data = iCartService.getVoByUid(getuidFromSession(session));
        return new JsonResult<>(OK,data);
    }
    @RequestMapping("{cid}/num/add")
        public JsonResult<Integer> addNum(@PathVariable("cid") Integer cid, HttpSession session){
        Integer data = iCartService.addNum(cid, getuidFromSession(session), getUsernameFromSession(session));
        return new JsonResult<>(OK,data);
    }
    @GetMapping("list")
    public JsonResult<List<CartVo>> getVOByCids(Integer[] cids, HttpSession session) {
        // 从Session中获取uid
        Integer uid = getuidFromSession(session);
        // 调用业务对象执行查询数据
        List<CartVo> data = iCartService.getVOByCids(uid, cids);
        // 返回成功与数据
        return new JsonResult<>(OK, data);
    }
}
